package com.yanqu.road.web.controller.usercenter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ChannelVersion;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.web.HttpGameWebParam;
import com.yanqu.road.server.manger.ChannelVersionMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.web.controller.BaseGameHttpServlet;
import com.yanqu.road.web.controller.usercenter.logic.ServerListHelper;
import com.yanqu.road.web.util.IpAddressHelper;
import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/newServerListNotice")
public class NewChannelServerList extends BaseGameHttpServlet {

	private static Logger logger = LogManager.getLogger(NewChannelServerList.class.getName());

	private static final long serialVersionUID = 2120129387060291974L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		/**
		 * 参数解析
		 */
		logger.info("request newServerListNotice");
		HttpGameWebParam httpGameWebParam = super.parseRequest(request);
		JSONObject paramObj = httpGameWebParam.getParamObj();
		String param = httpGameWebParam.getParam();
		String language = httpGameWebParam.getLanguage();

		int channelId;
		String openId;
		String platformUserId;
		String packageVersion;
		int phonePlatform;
		try {
			channelId = paramObj.getInteger("channelId");
			openId = paramObj.getString("openId");
			platformUserId = paramObj.getString("platformUserId");
			packageVersion = paramObj.getString("packageVersion");
			phonePlatform = paramObj.getIntValue("phonePlatform");
		} catch (Exception e) {
			sendMessage(response, getResultObj(GameErrorCode.E_REQUEST_ARGUMENT_ERROR, language).toJSONString());
			logger.error("json格式出错！ json 字符串param为{}", param, e);
			return;
		}

		String ip = IpAddressHelper.getIpAddr(request);

		JSONObject resultObj = getResultObj(0, language);

		/**
		 * 设置提审状态
		 */
		ChannelVersion channelVersion = ChannelVersionMgr.getChannelVersion(channelId, phonePlatform, packageVersion);
		if (null != channelVersion) {
			resultObj.put("isTishen", channelVersion.isReview());
		}

		/**
		 * 设置客户端版本
		 */
		ChannelVersion lastVersion = ChannelVersionMgr.getLastReleaseVersion(channelId, phonePlatform, ip, false, platformUserId);
		if (null != lastVersion) {
			resultObj.put("clientVersion", lastVersion.getClientVersion());
		}

		/**
		 * 获取服务器列表
		 */
		List<ServerInfo> serverList = ServerListMgr.getChannelServerList(channelId, ip, channelVersion, platformUserId);

		/**
		 * 填充区服列表、渠道公告、分流处理
		 */
		ServerListHelper.dealServerList(resultObj, ip, openId, channelId, serverList, language, true, false,new ArrayList<>(), "");

		sendMessage(response, Base64.encodeBase64String(resultObj.toJSONString().getBytes("utf-8")));
	}
}
